package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ProjectAttachmentsDao;
import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.TaskDao;
import com.rstrulak.dao.UserProjectDao;
import com.rstrulak.model.Project;
import com.rstrulak.model.ProjectAttachments;
import com.rstrulak.model.Task;
import com.rstrulak.model.UserProject;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("projectBean")
@Scope("request")
public class ProjectBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8581511512776537100L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private TaskDao taskDao;

	@Autowired
	private UserProjectDao userProjectDao;

	@Autowired
	private ProjectAttachmentsDao projectAttachmentsDao;

	private Project project;

	private List<Task> taskList = null;

	private List<UserProject> userList = null;

	private Long projectId;

	@SuppressWarnings("unused")
	private Boolean projectOwner;

	private List<ProjectAttachments> projectAttachmentsList;

	@PostConstruct
	public void init() {
		projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);

		if (projectId == null) {
			JSFUtils.setFlashFromSession();
			projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
			JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		}
		// Jeśli przestana dzialas flashscopre dla Context nalezy to wywalic
		JSFUtils.reloadFlash();
		//
		getProjectFromDb(projectId);
		getTaskListFromDb(project);
		getUserListFromDb(project);

		if (project != null) {
			projectAttachmentsList = projectAttachmentsDao.getProjectAttachemntsListByProject(project);
		}
	}

	@PreDestroy
	public void destroy() {
		Long _projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, _projectId);
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	
	public List<ProjectAttachments> getProjectAttachmentsList() {
		return projectAttachmentsList;
	}

	public void setProjectAttachmentsList(List<ProjectAttachments> projectAttachmentsList) {
		this.projectAttachmentsList = projectAttachmentsList;
	}

	public List<UserProject> getUserList() {
		return userList;
	}

	public void setUserList(List<UserProject> userList) {
		this.userList = userList;
	}

	private void getProjectFromDb(Long projectId) {
		this.project = projectDao.getById(projectId);
	}

	/**
	 * Check if user which is currently logged is user of the project
	 * 
	 * @return
	 */

	public Boolean getProjectOwner() {
		return JSFUtils.getLoggedUser().getId().equals(project.getOwner().getId());
	}

	public void setProjectOwner(Boolean projectOwner) {
		this.projectOwner = projectOwner;
	}

	private void getTaskListFromDb(Project project) {
		taskList = taskDao.getTaskListByProject(project);
	}

	private void getUserListFromDb(Project project) {
		userList = userProjectDao.getUserListByProject(project, JSFUtils.getLoggedUser());
	}

	public void showContextDetails(Long contextId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, contextId);
		JSFUtils.redirect("/protected/contextDetails.xhtml");
	}

	public void showTaskDetails(Long taskId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		JSFUtils.redirect("/protected/taskDetails.xhtml");
	}

	public void goToCreateTaskSite() throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		JSFUtils.redirect("/protected/newTask.xhtml");
	}

	public void goToAssignUserSite() throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		JSFUtils.redirect("/protected/assignUser.xhtml");
	}

	public void goToEditProjectSite() throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		JSFUtils.redirect("/protected/editProject.xhtml");
	}

	public void deleteTask(Long taskId) throws ServletException, IOException {
		taskDao.delete(taskId);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		getTaskListFromDb(project);
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void deleteUserFromProject(UserProject userProject) throws ServletException, IOException {
		userProjectDao.delete(userProject);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		getTaskListFromDb(project);
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

}
